/*
 *  cylk.h
 *  cylk
 *
 *  Created by Benjamin Heasly on 7/6/09.
 *  Copyright 2009 benjamin.heasly@gmail.com. Seattle, WA.  All rights reserved.
 *
 *  Luke Bloy and Rishi Kalwani created an AFNI plugin called "cyl", which finds 
 *  an elliptical region and projects a cylindrical shape through the volumen of 
 *  an MRI image.
 *
 *  The plugin worked, but was trapped on a linux machine.  I have a few goals:
 *    - build the plugin on OS X, in XCode, so that it's easier to access and use.
 *    - clean up the source code, which is really a mess.  Organize in to 5 groups:
 *			- plug_cylk.c, which defines the interface with AFNI
 *			- cylk.h and cylk.c, which define all the plugin actual behavior
 *			- cylk_basicUtilities.h and .c, with some baic math functions
 *			- cylk_imageManagement.h and .c, with some datatypes and functions for 2D and 3D images
 *			- cylk_regionDetection.h and .c, with some datatypes and functions for detecting 3D a region
 *	  - apply a loose naming convention to variables and functions so they're actually readable.
 *    - clearout deadwood functions that are unused and confusing.
 *    - remove gtk/gdk gui dependencies.  Do all interface and plotting with AFNI itself.
 *	  - break out global and hardcoded variables as AFNI options in the AFNI GUI.
 *	  - rename the plugin to "cylk", which is a sweeter name, to reflect nicer implementation.
 */

#ifndef CYLK
#define CYLK

#include <stdio.h>
#include <math.h>
#include <string.h>

#include "cylk_imageManagement.h"
#include "afni.h"

// ******************************************
// Working with AFNI datasets
// ******************************************
void runCylk(THD_3dim_dataset *inputDataSet,
			 PLUGIN_interface *plint,
			 int do_projection,
			 float fit_tolerance,
			 int do_rotation, 
			 int seed_at_max_voxel, 
			 float region_thresh_pct, 
			 int xSeed,
			 int ySeed, 
			 int zSeed);

THD_3dim_dataset* copyDatasetWithNameReplaceBrickWithImage(THD_3dim_dataset *inputDataSet, 
														   PLUGIN_interface *plint,
														   char* name, 
														   cylkImage *im);

void rotateImageVolume(cylkImage *im, 
					   float *rotateABC, 
					   float *shiftXYZ);

cylkImage new_cylkImageFromAFNIDataset(THD_3dim_dataset* dSet);


// ******************************************
// Doing Cylinder Projection
// ******************************************
int projectCylinderWithFitTolerance(cylkImage *dataIm,
									cylkImage *projectionIm, 
									float tolerance, 
									float *r,
									float *phi,
									float *tilt, 
									float *anchor);

int countGoodEllipseFitsForImageDimensionWithTolerance(cylkImage *im, int dim, float tolerance);

float errorOfEllipseFitToSlice(cylkSlice *sl,
							   float *r, 
							   float *phi, 
							   float *tilt, 
							   float *centX, 
							   float *centY, 
							   float *ecc);

int buildCylinderWithDimensionAndFitTolerance(cylkImage *dataIm,
											  cylkImage *projectIm, 
											  int dim, 
											  float tolerance, 
											  float *rOut, 
											  float *phiOut, 
											  float *tiltOut, 
											  float *anchor);

cylkSlice new_cylkSliceLikeSliceWithFilledEllipse(cylkSlice *like,
												  float majorAxis, 
												  float minorAxis, 
												  float phi,
												  float centerX, 
												  float centerY);
#endif